% Exemples d'utilisation d'UIFIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% 1) Search parametres which fit x,y series with a real function y(x)
% -------------------------------------------------------------------

	x=0:.2:3;
	y=2*exp(-x.^3);
   uifit(x,y,'p(1)*exp(-x.^p(2))',[1,0])
   pause

% 2) Search parametres which fit x,y series with a complex function y(x)
% ----------------------------------------------------------------------

	% rem: UIfit dispaly only the absolute value of complex results

	x=0:.2:3;
	y=2*exp(-x.^3)+j*2.*x;
	uifit(x,y,'p(1)*exp(-x.^p(2))+j*p(3).*x',[1,0])
   pause

% 3) Use specials functions (non exhaustive cases)
% ------------------------------------------------

	% a) Step(num,den,t) with an offset

	x=(0:1:15);
	y=step([1],[.2 .1 1],x)+.5;
	uifit(x,y,'step([p(1)],[p(2) p(3) p(4)],x)+p(5)')
   pause

	% b) Bode(num,den,w) witch w as a vector of datas

	w=(0:.5:4);
	y=bode([1],[.2 .1 1],w);
	uifit(w,y,'bode([p(1)],[p(2) p(3) p(4)],x)',[1 2 3 4])
   pause
   
   % c) tune pi regulator
   
   num=1;
   den=conv(conv([1 12],[1 12]),[1 12]);
   x=(0:.1:1.2);
   y=ones(1,length(x));
   uifit(x,y,'step(cloop([p(1).*p(2) p(1)],p(2).*conv(conv([1 12],[1 12]),[1 12])),x)')
   input('param:','s');
   p=ans;
   step([p(1).*p(2) p(1)],p(2).*conv(conv([1 12],[1 12]),[1 12]))
   
   pause

% 4) solve equation of one variable like 'sin(2*x)+cos(x)=.5' arround 6
% ---------------------------------------------------------------------

	% rem: input value for x must be 0
   %	    input value for y is the function goal
   %	    the drawing show a point and the goal

	x=(0:.1:10); y=sin(2*x)+cos(x);	plot(x,y); hold on; plot([0 10],[.5 .5],'r'); hold off

	uifit(0,.5,'sin(2*p(1))+cos(p(1))',[6],1e-8)
   pause
   
   
% 5) solve equation of two (or more) variables like 'sin(2*a)+cos(b)=.5' arround [6,6]
% ------------------------------------------------------------------------------------

	% rem: find only one solution!

	[a,b]=meshgrid(0:.2:8,0:.2:8);
	y=sin(2*a)+cos(b);
	z=.5*ones(size(a),size(b));
	mesh(a,b,z); hold on; surfl(a,b,y);
	colormap('copper'); shading('interp');
	[x,y,z]=sphere(10);
	x=x/10+.54; y=y/10+4.32; z=z/10+.5;
	surfl(x,y,z); shading('interp');
	hold off
	grid

	uifit(0,.5,'sin(2*p(1))+cos(p(2))',[0.5,4],1e-8)
